﻿using Abide_AddOn_API.Interfaces;
using Abide_Halo_Library;
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Abide_AddOn_API.Controls
{
    /// <summary>
    /// Provides an empty TabPage Abide AddOn.
    /// </summary>
    [ToolboxItem(false), ComVisible(true)]
    public class TabPage : UserControl, ITabPage
    {
        private event EventHandler initialize;
        private event EventHandler xboxChanged;
        private event EventHandler indexEntryChanged;
        private event EventHandler nameChanged;

        /// <summary>
        /// Occurs when the AddOn's name is changed.
        /// </summary>
        [Browsable(true), Category("Property Changed"), Description("Occurs when the tool's name is changed.")]
        public new event EventHandler TextChanged
        {
            add { nameChanged += value; }
            remove { nameChanged -= value; }
        }
        /// <summary>
        /// Occurs when the Debug Xbox is changed
        /// </summary>
        [Browsable(true), Category("Abide Events"), Description("Occurs when the Debug Xbox is changed")]
        public event EventHandler XboxChanged
        {
            add { xboxChanged += value; }
            remove { xboxChanged -= value; }
        }
        /// <summary>
        /// Occurs when the selected Index Entry is changed
        /// </summary>
        [Browsable(true), Category("API Events"), Description("Occurs when the selected Index Entry is changed")]
        public event EventHandler IndexEntryChanged
        {
            add { indexEntryChanged += value; }
            remove { indexEntryChanged -= value; }
        }
        /// <summary>
        /// The Halo Map version this Abide AddOn is designed to work with
        /// </summary>
        [Browsable(true), Category("AddOn Properties"), Description("The Halo Map version this Abide AddOn is designed to work with")]
        public MapVersion MapVersion
        {
            get { return mapVersion; }
            set { mapVersion = value; }
        }
        /// <summary>
        /// The version of Abide this AddOn is designed to work with
        /// </summary>
        [Browsable(true), Category("AddOn Properties"), Description("The version of Abide this AddOn is designed to work with")]
        [TypeConverter(typeof(Design.VersionTypeConverter))]
        public Version AbideVersion
        {
            get { return abideVersion; }
            set { abideVersion = value; }
        }
        /// <summary>
        /// The Author of the Abide AddOn
        /// </summary>
        [Browsable(true), Category("AddOn Properties"), Description("The Author of the Abide AddOn")]
        public string Author
        {
            get { return author; }
            set { author = value; }
        }
        /// <summary>
        /// The version of this Abide AddOn
        /// </summary>
        [Browsable(true), Category("AddOn Properties"), Description("The version of this Abide AddOn")]
        [TypeConverter(typeof(Design.VersionTypeConverter))]
        public Version Version
        {
            get { return version; }
            set { version = value; }
        }
        /// <summary>
        /// The Abide AddOn's Display Tag Class Filter
        /// </summary>
        [Browsable(true), Category("AddOn Properties"), Description("The Abide AddOn's Display Tag Class Filter")]
        public string Filter
        {
            get { return filter; }
            set { filter = value; }
        }
        /// <summary>
        /// The Abide AddOn's Display Text
        /// </summary>
        [Browsable(true), Category("Appearance"), Description("The Abide AddOn's Display Text")]
        public string DisplayName
        {
            get { return text; }
            set
            {
                bool trigger = text != value;
                text = value;
                if (trigger)
                {
                    nameChanged?.Invoke(this, new EventArgs());
                    OnTextChanged(new EventArgs());
                }
            }
        }
        /// <summary>
        /// The Abide AddOn Application Programming Interface this Abide AddOn will use
        /// </summary>
        [Browsable(false)]
        public AddOnAPI API
        {
            get { return api; }
        }

        private MapVersion mapVersion = MapVersion.Halo_2;
        private Version abideVersion = AddOnAPI.GetAbideVersion();
        private Version version = new Version(1, 0);
        private string author = "Abide";
        private string text = "TabPage";
        private string directory = string.Empty;
        private string filter = "*";
        private AddOnAPI api = null;

        event EventHandler ITabPage.NameChanged
        {
            add { nameChanged += value; }
            remove { nameChanged -= value; }
        }
        string ITabPage.Name
        {
            get { return text; }
        }
        string ITabPage.Filter
        {
            get { return filter; }
        }
        UserControl ITabPage.Interface
        {
            get { return this; }
        }
        AddOnAPI IAddOn.API
        {
            get { return api; }
        }
        MapVersion IAddOn.MapVersion
        {
            get { return mapVersion; }
        }
        Version IAddOn.Version
        {
            get { return version; }
        }
        Version IAddOn.AbideVersion
        {
            get { return abideVersion; }
        }
        string IAddOn.Author
        {
            get { return author; }
        }
        string IAddOn.Directory
        {
            get { return directory; }
            set { directory = value; }
        }

        protected TabPage() { }
        protected override void OnTextChanged(EventArgs e)
        {
            //Throw Event
            nameChanged?.Invoke(this, e);
        }
        protected virtual void OnInitialize(IAddOnHost host)
        {
            //Initialize API
            api = new AddOnAPI(host, this);
        }
        protected virtual void OnXboxChanged(EventArgs e)
        {
            //Throw Event
            xboxChanged?.Invoke(this, e);
        }
        protected virtual void OnIndexEntryChanged(EventArgs e)
        {
            //Throw Event
            indexEntryChanged?.Invoke(this, e);
        }
        void IAddOn.Initialize(IAddOnHost host)
        {
            //Virtual Void
            OnInitialize(host);

            //Throw Event
            initialize?.Invoke(this, new EventArgs());
        }
        void IAddOn.OnXboxChanged()
        {
            //Virtual Void
            OnXboxChanged(new EventArgs());
        }
        void IAddOn.OnIndexEntryChanged()
        {
            //Virtual Void
            OnIndexEntryChanged(new EventArgs());
        }
        void IDisposable.Dispose()
        {
            //Base Dispose
            Dispose();
        }
    }
}
